<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" :rules="ruleForm.task_duty==0?rules:rules1" label-width="100px" class="demo-ruleForm">
      <el-form-item label="执行" prop="task_duty">
        <el-radio-group v-model="ruleForm.task_duty">
          <el-radio label="0">班组</el-radio>
          <el-radio label="1">个人</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="ruleForm.task_duty==0" label="运维班组" prop="group_user">
        <el-select v-model="ruleForm.group_user" placeholder="请选择运维班组">
          <el-option
            v-for="item in group_user"
            :key="item.id"
            style="width:400px"
            :label="item.longName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-show="ruleForm.task_duty==1" label="运维人员" prop="group_users">
        <el-select v-model="ruleForm.group_users" placeholder="请选择运维班组">
          <el-option
            v-for="item in g_users"
            :key="item.id"
            style="width:400px"
            :label="item.longName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设置超时" prop="task_checktime">
        <el-radio-group v-model="ruleForm.task_checktime">
          <el-radio label="是" />
          <el-radio label="否" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="超时时间(m)" prop="task_minute">
        <el-input v-model="ruleForm.task_minute" type="number" />
      </el-form-item>
      <el-form-item v-show="ruleForm.task_duty==1" label="是否用车" prop="task_car">
        <el-radio-group v-model="ruleForm.task_car">
          <el-radio label="是" />
          <el-radio label="否" />
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="ruleForm.task_duty==1" label="司机" prop="task_driver">
        <el-select v-model="ruleForm.task_driver" placeholder="请选择">
          <el-option
            v-for="item in drivers"
            :key="item.id"
            style="width:400px"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <div class="btnBox">
        <el-button class="btn" type="primary" size="mini" @click="submitForm('ruleForm')">确认</el-button>
        <el-button class="btn" type="primary" size="mini" @click="closed('ruleForm')">关闭</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import { sendCards, workerdrevers } from '@/api/order'

export default {
  name: 'send',
  props: ['group_user', 'g_users', 'accids', 'lists'],
  data() {
    return {
      ruleForm: {
        task_duty: '',
        group_user: '',
        group_users: '',
        task_checktime: '',
        task_minute: '',
        task_car: '',
        task_driver: ''
      },
      rules: {
        task_duty: [
          { required: true, message: '请选择执行类别', trigger: 'change' }
        ],
        group_user: [
          { required: true, message: '请选择运维班组', trigger: 'change' }
        ],
        task_checktime: [
          { required: true, message: '请设置超时', trigger: 'change' }
        ],
        task_minute: [
          { required: true, message: '请输入超时时间', trigger: 'blur' }
        ]
      },
      rules1: {
        task_duty: [
          { required: true, message: '请选择执行类别', trigger: 'change' }
        ],
        group_users: [
          { required: true, message: '请选择运维班组', trigger: 'change' }
        ],
        task_checktime: [
          { required: true, message: '请设置超时', trigger: 'change' }
        ],
        task_minute: [
          { required: true, message: '请输入超时时间', trigger: 'blur' }
        ],
        task_car: [
          { required: true, message: '请选择是否用车', trigger: 'change' }
        ]
      },
      closeds: false,
      accidsWatchs: '',
      drivers: '',
      isChange: 1

    }
  },
  watch: {
    accids() {
      this.accidsWatchs = this.accids
    }
  },
  created() {
    this.getDriver()
  },
  methods: {
    submitForm(formName) {
      const a = this.ruleForm.task_duty
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (a == 0) {
            const data = {}
            data.id = this.accids
            data.task_status = this.lists.task_status
            data.task_duty = this.ruleForm.task_duty
            data.group_user = this.ruleForm.group_user.toString()
            if (this.ruleForm.task_checktime == '否') {
              data.task_checktime = '0'
            } else {
              data.task_checktime = '1'
            }
            data.task_minute = this.ruleForm.task_minute
            sendCards(data).then(res => {

            })
          } else {
            const data = {}
            data.task_duty = this.ruleForm.task_duty
            data.group_user = this.ruleForm.group_users
            data.id = this.accids
            data.task_status = this.lists.task_status
            if (this.ruleForm.task_checktime == '否') {
              data.task_checktime = '0'
            } else {
              data.task_checktime = '1'
            }
            data.task_minute = this.ruleForm.task_minute
            if (this.ruleForm.task_car == '否') {
              data.task_car = '0'
            } else {
              data.task_car = '1'
            }
            data.task_driver = this.ruleForm.task_driver
            sendCards(data).then(res => {
              console.log(8888)
            })
          }
          this.isChange = 1
          this.$emit('detailesee', this.closeds, this.isChange)
          // this.$emit('changeTab', )
          this.ruleForm.task_car = ''
          this.ruleForm.group_users = ''
          this.ruleForm.task_duty = ''
          this.ruleForm.group_user = ''
          this.ruleForm.task_minute = ''
          this.ruleForm.task_checktime = ''
          this.ruleForm.task_driver = ''
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 司机列表
    getDriver() {
      workerdrevers({
        sort: 'created_at',
        offset: 1,
        limit: 1,
        order: 'ascending',
        group_name: '',
        role_ids: [7]
      }).then(res => {
        this.drivers = res.data.admins
      })
    },
    closed() {
      console.log(this.closeds)
      this.isChange = 2
      this.$emit('detailesee', this.closeds, this.isChange)
    }
  }
}
</script>
<style lang="scss" scoped>
.btnBox{
  width: 100%;
  display: flex;
  flex-direction: row-reverse;
  margin-top: 20px;
  // border-top:1px solid #eee;
}
.btn{
  margin-left: 10px;
}
</style>
